package com.seven.gitautopush.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import lombok.Data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;

@Data
public class GitConfig implements Serializable {
    private GitLog log;
    private GitDb db;
    private List<String> ignoreDir;
    private GitAuto auto;
    static GitConfig gitConfig;

    /**
     * 默认配置
     */
    static {
        gitConfig = JSONUtil.toBean("{\n" +
                "  \"auto\": {\n" +
                "    \"push\": {\n" +
                "      \"cron\": \"0/10 * * * * ?\",\n" +
                "      \"flag\": \"true\"\n" +
                "    },\n" +
                "    \"search\": {\n" +
                "      \"cron\": \"0/10 * * * * ?\",\n" +
                "      \"flag\": \"true\"\n" +
                "    }\n" +
                "  },\n" +
                "  \"log\": {\n" +
                "    \"path\": \"./logs\"\n" +
                "  },\n" +
                "  \"ignoreDir\": [\n" +
                "    \".idea\",\n" +
                "    \"src\",\n" +
                "    \".git\",\n" +
                "    \".mvn\",\n" +
                "    \".github\",\n" +
                "    \"docker\",\n" +
                "    \"build\",\n" +
                "    \"target\",\n" +
                "    \"node_modules\",\n" +
                "    \"node\"\n" +
                "  ],\n" +
                "  \"db\": {\n" +
                "    \"path\": \"./db\"\n" +
                "  }\n" +
                "}", GitConfig.class);
    }

    public GitConfig() {
    }

    public static GitConfig getGitConfig() {
        return gitConfig;
    }

    public static void initConfig(String properties) throws IOException {
        Props props = PropsUtil.get(properties);
        gitConfig = props.toBean(GitConfig.class, "git");
    }
}
